/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commons.beanutils.PropertyUtils;

import common.Person;
import commons.beanutils.*;
import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.functors.NotNullPredicate;

/**
 *
 * @author WangShuai
 */
public class Demo1 {

    public static void main(String[] args) throws Exception{
        
        //1.PropertyUtils.getSimpleProperty  通过此方法获得数据与直接调用getter/setter方法相比，优点在于可以动态的调用JavaBean的方法：
        //Don't locks you into a specific bean property at compile time.
        //you may need to access an arbitrary property of an arbitrary object known only at runtime.
//        Person person = new Person();
//        person.setName("Alex Wolfe");
//        person.setFavoriteColor("Green");
//        try {
//            String name = (String) PropertyUtils.getSimpleProperty(person, "name");
//            String favoriteColor =
//                    (String) PropertyUtils.getSimpleProperty(person, "favoriteColor");
//            System.out.println("The Person: " + name + " likes " + favoriteColor);
//        } catch (IllegalAccessException e) {
//            System.out.println("You are not allowed to access a property!");
//        } catch (InvocationTargetException e) {
//            System.out.println("There was a problem invoking the method.");
//        } catch (NoSuchMethodException e) {
//            System.out.println("There is no method to get a property.");
//        }

        //2.PropertyUtils.copyProperties()
//        Person person = new Person();
//        Map mapProps = new HashMap( );
//        mapProps.put( "name", "The Art of Computer Programming" );
//        mapProps.put( "favoriteColor", "Red" );      //注释后：The Person: The Art of Computer Programming likes null
//        mapProps.put(" sex", "man");   //多余属性
//        PropertyUtils.copyProperties( person, mapProps );
//        System.out.println("The Person: " + person.getName() + " likes " + person.getFavoriteColor());
//
//        User user = new User();
//        PropertyUtils.copyProperties(user, person);
//        System.out.println("The User: " + user.getName() + " is " + user.getDescr());

        //3.BeanPredicate
        Predicate teamNotNull = new BeanPredicate( "name", NotNullPredicate.getInstance() );
        Person person = new Person();
        System.out.println(teamNotNull.evaluate(person));
    }
}
